package com.fake_Interviews.controller;

import com.fake_Interviews.Mapper.FeedbackMapper;
import com.fake_Interviews.Mapper.InterviewsMapper;
import com.fake_Interviews.module.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class JobSeekersFeedback_controller {
    private final FeedbackMapper feedbackMapper;
    private final InterviewsMapper interviewsMapper;
    public Integer id;

    public JobSeekersFeedback_controller(FeedbackMapper feedbackMapper, InterviewsMapper interviewsMapper) {
        this.feedbackMapper = feedbackMapper;
        this.interviewsMapper = interviewsMapper;
    }

    @PostMapping("/Feedbackfromjobseekers")
    public ResponseEntity<String> intervieweeFeedback (@RequestBody Map<String,String> data) {
        Feedback feedback = new Feedback();
        // 拿到数据库Feedback里所有的数据
        FeedbackExample example = new FeedbackExample();
        List<Feedback> list = feedbackMapper.selectByExample(example);

        // 第几场面试
        id = Integer.parseInt(data.get("id"));

        // 根据第几场面试的id来判断是不是存在，如果存在，就直接修稿对应ID的反馈字段，否则创建新的面试场次
        for (int i = 0; i < list.size(); i++) {
            if(id == list.get(i).getInterviewsId()) {
                feedback.setFeedbackfromjobseekers(data.get("feedbackfromjobseekers"));
                example.createCriteria().andIdEqualTo(id);
                feedbackMapper.updateByExampleSelective(feedback,example);
                return ResponseEntity.ok().header("Custom-Header","ok").body("提交成功！");
            }
        }

        feedback.setId(id);
        feedback.setInterviewsId(id);
        feedback.setFeedbackfromjobseekers(data.get("feedbackfromjobseekers"));
        feedbackMapper.insert(feedback);
        return ResponseEntity.ok().header("Custom-Header","ok").body("提交成功！");
    }

}
